//
//  MLSStatusCell.swift
//  MLSMircoBlog
//
//  Created by 袁航 on 15/8/2.
//  Copyright © 2015年 yuanhang. All rights reserved.
//

import UIKit
enum statusCellType: String {
    case transCell = "transCell"
    case normallCell = "normallCell"
    
    // 根据模型,返回cell 重用标识符
    static func cellType(status: MLSUserStatus) -> String {
        return status.retweeted_status == nil ? statusCellType.normallCell.rawValue : statusCellType.transCell.rawValue
    }
}
// 全局共享,控件间隙
let separatorMargin: CGFloat = 8
// MARK:- 使用的数字常量
private let separatorLineViewHeight: CGFloat = 10
private let headViewHeight: CGFloat = 55
private let bottomViewHeight: CGFloat = 44
private let detailLabelFontSize: CGFloat = 15

class MLSStatusCell: UITableViewCell {

    // 记录pictureView 的约束(动态改变)
    var pictureViewHeightCons: NSLayoutConstraint?
    var pictureViewWidthCons: NSLayoutConstraint?
    var pictureViewTopCons: NSLayoutConstraint?
    // 模型数据
    var status: MLSUserStatus? {
        didSet{
            headView.status = status
            bottomView.status = status
            pictureView.status = status
            detailLabel.text = status?.text ?? ""
            // 重新设置约束
            pictureViewWidthCons?.constant = pictureView.frame.width
            pictureViewHeightCons?.constant = pictureView.frame.height
            pictureViewTopCons?.constant = (pictureView.frame.height == 0 ? 0 : separatorMargin)
        }
    }
    // 计算行高
    func rowHeight(status: MLSUserStatus) -> CGFloat {
        self.status = status
        // 强制更新布局
        layoutIfNeeded()
        let height = CGRectGetMaxY(bottomView.frame)
        status.rowHeight = height
        return height
    }
    // 重写init方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 设置背景颜色
        contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
        // 设置UI
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        // 添加控件
        // 头部分割线
        contentView.addSubview(separatorLineView)
        contentView.addSubview(headView)
        contentView.addSubview(detailLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        // 布局控件
        // 1> 顶部分割线
        separatorLineView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: separatorLineViewHeight))
        // 2> 头部视图
        headView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: separatorLineView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: headViewHeight))
        
        // 3> 文本视图
        detailLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: headView, size: nil, offset: CGPoint(x: separatorMargin, y: separatorMargin))
        contentView.addConstraint(NSLayoutConstraint(item: detailLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * separatorMargin))
        
        // 4> 底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: bottomViewHeight), offset: CGPoint(x: -separatorMargin, y: separatorMargin))
    }
    
    // 懒加载控件
    private lazy var separatorLineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.9, alpha: 1)
        return view
    }()
    private lazy var headView: MLSCellTopView = MLSCellTopView()
    // 当手动计算cell的行高的时候,必须要指定label的最大换行宽度,否则会导致行高计算不准确
    lazy var detailLabel: FFLabel = {
        let label = FFLabel(color: UIColor.darkGrayColor(), fontSize: detailLabelFontSize)
        // label的最大换行宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * separatorMargin
        label.numberOfLines = 0
    return label
    }()
    lazy var bottomView: MLSBottomView = MLSBottomView()
    lazy var pictureView: MLSPictureView = MLSPictureView()
}
